package com.brother.alarm;

import java.util.List;

import com.brother.model.Alarm;
import com.brother.model.AlarmType;
import com.brother.model.LatLngPoint;
import com.brother.model.RealTimeData;
import com.brother.service.AlarmService;
import com.brother.service.RealDataService;
import com.brother.util.PointIntersect;
import com.ibatis.common.util.PaginatedList;

public class AlarmObserverImpl implements AlarmObserver {

	private int alarmId = 0;
	private RealTimeData realtimeData;
	@Override
	public AlarmType getTriggerredAlarm() {
		if(realtimeData == null || realtimeData.getTrackerid() == null){
			return AlarmType.GPS_Pos;
		}
		Alarm alarm = new Alarm();
		alarm.setTrackerId(realtimeData.getTrackerid());
		AlarmService als = new AlarmService();
		List<Alarm> alarms = als.findAlarms(alarm);
		AlarmType type = getGeogence(alarms);
		if(type == AlarmType.GPS_Pos){
			//type = this.getSpeedAlarm(alarms);
		}
		return type;
	}

	/**获取速度报警信息，如果已经超速则返回速度报警。
	 * @param alarms
	 * @return
	 */
	private AlarmType getSpeedAlarm(List<Alarm> alarms) {
		for(Alarm alarm: alarms){
			if(alarm.getAlarmType().equals(AlarmType.Over_Speed_Alarm.toString())){
				float limitSpeed = alarm.getValue1();
				if(this.realtimeData.getSpeed() > limitSpeed){
					return AlarmType.Over_Speed_Alarm;
				}
			}
		}
		return AlarmType.GPS_Pos;
	}

	private AlarmType getGeogence(List<Alarm> alarms) {
		 for(Alarm alarm: alarms){
	            if(alarm.getAlarmType().equals(AlarmType.Geo_Fence_Alarm.toString()) 
	            		&& !alarm.getState().equals("0"))
	            {
	                String data = alarm.getData();
	                RealDataService rds = new RealDataService();
	                RealTimeData search = new RealTimeData();
	                search.setTrackerid(realtimeData.getTrackerid());
	                search.setState(AlarmType.Geo_Fence_Alarm.name());
	                search.setAlarmId(alarm.getAlarmId());
	                search.setOrderCondition(" order by tracetime desc");
	                PaginatedList pageData = rds.findData("0", 10, search);
	                if(pageData.size() <= 0){ // 该报警之前没有
	                    if(alarm.getCondition().equals("0"))
	                    {
	                        if(!isInArea(data, realtimeData))
	                        {
	                            alarmId = alarm.getAlarmId();
	                            return AlarmType.Geo_Fence_Alarm;
	                        }
	                    } else if(alarm.getCondition().equals("1") && isInArea(data, realtimeData))
	                    {
	                        alarmId = alarm.getAlarmId();
	                        return AlarmType.Geo_Fence_Alarm;
	                    }
	                }
	            }
	        }

	        return AlarmType.GPS_Pos;

	}

	/**判断是否在这个区域内
	 * @param data
	 * @param realTimeData2 
	 * @return
	 */
	private boolean isInArea(String data, RealTimeData realTimeData2) {
		/*
		String min = data.split(";")[0];
		String max = data.split(";")[1];
		float minLng = Float.parseFloat(min.split(",")[0]);
		float minLat = Float.parseFloat(min.split(",")[1]);
		float maxLng = Float.parseFloat(max.split(",")[0]);
		float maxLat = Float.parseFloat(max.split(",")[1]);
		if(realTimeData2.getLng() - minLng > 0  && realTimeData2.getLat() - minLat > 0 &&
				realTimeData2.getLng() - maxLng < 0 && realTimeData2.getLat() - maxLat < 0){
			return true;
		}
		return false;
		*/
       /* String min = data.split(";")[0];
        String max = data.split(";")[1];
        float minLng = Float.parseFloat(min.split(",")[0]);
        float minLat = Float.parseFloat(min.split(",")[1]);
        float maxLng = Float.parseFloat(max.split(",")[0]);
        float maxLat = Float.parseFloat(max.split(",")[1]);
        int lngSign = realTimeData2.getLngOrient().equals("W") ? -1 : 1;
        int latSign = realTimeData2.getLatOrient().equals("S") ? -1 : 1;
        if((float)lngSign * realTimeData2.getLng() - minLng > 0.0F 
        		&& (float)latSign * realTimeData2.getLat() - minLat > 0.0F 
        		&& (float)lngSign * realTimeData2.getLng() - maxLng < 0.0F 
        		&& (float)latSign * realTimeData2.getLat() - maxLat < 0.0F)
        	return true;
        
        return false;*/
        int lngSign = realTimeData2.getLngOrient().equals("W") ? -1 : 1;
        int latSign = realTimeData2.getLatOrient().equals("S") ? -1 : 1;
//        System.out.println(data+"  "+(latSign * realTimeData2.getLat())+"  "+(lngSign * realTimeData2.getLng()));
		return PointIntersect.isInsidePolygon(data,
				new LatLngPoint(latSign * realTimeData2.getLat(), lngSign * realTimeData2.getLng()));

	}

	@Override
	public void setRealTimeData(RealTimeData data) {
		this.realtimeData = data;
	}

	@Override
	public int getAlarmId() {
		return this.alarmId;
	}

}
